package com.finley.flowabledemo.plc.controller;

import com.finley.flowabledemo.plc.service.S7Service;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/s7")
public class S7Controller {

    private final S7Service s7Service;

    public S7Controller(S7Service s7Service) {
        this.s7Service = s7Service;
    }

    /**
     * 读取布尔值
     * @param address 地址(如 "DB2.0.7" 或 "Q0.7")
     * @return 布尔值
     */
    @GetMapping("/boolean")
    public boolean readBoolean(@RequestParam String address) {
        return s7Service.readBoolean(address);
    }

    /**
     * 读取字节
     * @param address 地址(如 "DB2.1")
     * @return 字节值
     */
    @GetMapping("/byte")
    public byte readByte(@RequestParam String address) {
        return s7Service.readByte(address);
    }

    /**
     * 读取16位无符号整数
     * @param address 地址
     * @return 16位无符号整数
     */
    @GetMapping("/uint16")
    public int readUInt16(@RequestParam String address) {
        return s7Service.readUInt16(address);
    }

    /**
     * 读取32位浮点数
     * @param address 地址
     * @return 32位浮点数
     */
    @GetMapping("/float32")
    public float readFloat32(@RequestParam String address) {
        return s7Service.readFloat32(address);
    }

    /**
     * 写入布尔值
     * @param address 地址
     * @param value 要写入的值
     */
    @PostMapping("/boolean")
    public void writeBoolean(
            @RequestParam String address,
            @RequestParam boolean value) {
        s7Service.writeBoolean(address, value);
    }

    /**
     * 写入字节
     * @param address 地址
     * @param value 要写入的值
     */
    @PostMapping("/byte")
    public void writeByte(
            @RequestParam String address,
            @RequestParam byte value) {
        s7Service.writeByte(address, value);
    }

    /**
     * 写入16位无符号整数
     * @param address 地址
     * @param value 要写入的值
     */
    @PostMapping("/uint16")
    public void writeUInt16(
            @RequestParam String address,
            @RequestParam int value) {
        s7Service.writeUInt16(address, value);
    }

    /**
     * 写入32位浮点数
     * @param address 地址
     * @param value 要写入的值
     */
    @PostMapping("/float32")
    public void writeFloat32(
            @RequestParam String address,
            @RequestParam float value) {
        s7Service.writeFloat32(address, value);
    }
}
